<?php 
HeaderScripts::addStyles(array(
	'admin/grid.css'
));
?>
<?php echo FlashMessage::renderFlashInfoHtml() ?>
<div id="tabsStr">
	<?php foreach ($excursionTypes as $excursionType) { ?>
	<div id="gridStr<?php echo $excursionType['id']?>" class="x-hide-display">
		<div id="grid<?php echo $excursionType['id']?>"></div>
	</div>
	<?php } ?>
</div>
<script type="text/javascript">
<?php foreach ($excursionTypes as $excursionType) { ?>
var store<?php echo $excursionType['id']?> = null;
var grid<?php echo $excursionType['id']?>  = null;
<?php } ?>

Ext.onReady(function() {
<?php foreach ($excursionTypes as $excursionType) { ?>
	//create the Data Store
	store<?php echo $excursionType['id']?> = new Ext.data.JsonStore({
	    root: 'excursions',
	    totalProperty: 'totalCount',
	    idProperty: 'id',
	    remoteSort: true,
	
	    fields: [
	        'id', 'title', <?php if ($excursionType['id'] != 3) { ?>'price',<?php } ?> 'actions'
	    ],
	
	    proxy: new Ext.data.ScriptTagProxy({
	        url: '/admin/excursion/getdata/type/<?php echo $excursionType['id']?>/'
	    })
	});
	store<?php echo $excursionType['id']?>.setDefaultSort('title', 'asc');

	grid<?php echo $excursionType['id']?> = new Ext.grid.GridPanel({
	    width:600,
	    height:530,
	    title:'Экскурсии',
	    store: store<?php echo $excursionType['id']?>,
	    trackMouseOver:false,
	    disableSelection:true,
	    loadMask: {msg: 'Загрузка...'},
	
	    // grid columns
	    columns:[{
	        id: 'id',
	        header: "ID",
	        dataIndex: 'id',
	        width: 60,
	        sortable: true,
			align: 'center'
	    },{
	        header: "Название",
	        dataIndex: 'title',
			align: 'left',
	        sortable: true,
			width:300
	    },<?php if ($excursionType['id'] != 3) { ?>{
	        header: "Цена (руб)",
	        dataIndex: 'price',
			align: 'left',
	        sortable: true,
			width:120
	    },<?php } ?>{
	        header: "Действия",
	        dataIndex: 'actions',
			align: 'center',
	        width: 120,
	        sortable: false
	    }],
		
	    // customize view config
	    viewConfig: {
	        forceFit:true,
	        enableRowBody:true,
	        showPreview:false,
	        getRowClass : function(record, rowIndex, p, store){
	            if(this.showPreview){
	                p.body = '<p>'+record.data.excerpt+'</p>';
	                return 'x-grid3-row-expanded';
	            }
	            return 'x-grid3-row-collapsed';
	        }
	    },
	
	    // paging bar on the bottom
	    bbar: new Ext.PagingToolbar({
	        pageSize: 20,
	        store: store<?php echo $excursionType['id']?>,
	        displayInfo: true,
			beforePageText: 'Страница',
			afterPageText: 'из {0}',
	        displayMsg: 'Показаны {0} - {1} из {2}',
	        emptyMsg: "Нет данных",
	        items:['-']
	    }),
		
		tbar: [{
	        text: 'Создать',
			iconCls: 'silk-add',
	        handler : function(){
	            location.href = '/admin/excursion/create/';
	        }
	    }],
		
		sm: new Ext.grid.RowSelectionModel({singleSelect:true})
	});
<?php } ?>

	function gridActivate(tab)
	{
		var currentGrid = null;
		var tabNum = parseInt(tab.ctr);
		Ext.util.Cookies.set('sTabNum', tabNum - 1);
		eval('currentGrid = grid' + tabNum);

		if (!currentGrid.rendered) {
			currentGrid.render('grid' + tabNum);
			eval('store' + tabNum + '.load({params:{start:0, limit:20}})');
		}
	}

	var activeTab = parseInt(Ext.util.Cookies.get('sTabNum'));
	activeTab = activeTab ? activeTab : 0;

	Ext.DomHelper.insertFirst(document.body, {id:'msg-div-casper', cls:'ext-el-mask'}, true);

	var tabs = new Ext.TabPanel({
	    renderTo: 'tabsStr',
	    width:600,
	    activeTab: activeTab,
	    frame:true,
	    defaults:{autoHeight: true},
	    items:[
	        {
				ctr: 1,
				contentEl:'gridStr1',
				title: 'Групповые',
				listeners: {activate: gridActivate}
			},
	        {
				ctr: 2,
				contentEl:'gridStr2',
				title: 'Индивидуальные',
				listeners: {activate: gridActivate}
			},
			{
				ctr: 3,
				contentEl:'gridStr3',
				title: 'Экстримальные',
				listeners: {activate: gridActivate}
			}
	    ]
	});
});

function editExcursion(id)
{
	location.href = '/admin/excursion/edit/id/' + parseInt(id) + '/';
}

function deleteExcursion(id, typeId)
{
	Ext.MessageBox.buttonText.yes = 'Да';
	Ext.MessageBox.buttonText.no  = 'Нет';		
	Ext.MessageBox.confirm('Предупреждение!', 'Вы действительно хотите удалить экскурсию?', function (btn) {
		if (btn == 'yes') {
			Ext.Ajax.request({
   				url: '/admin/excursion/remove/id/' + parseInt(id) + '/',
   				success: function() {
					eval('store' + typeId + '.remove(store' + typeId + '.getById(parseInt(' + id + ')))');
					Ext.confpopup.msg('Экскурсия удалена', '');
					eval('store' + typeId + '.reload()');
				},
   				failure: function() {},
				disableCaching: false
			});
		}
	});
}
</script>